// pages/flea/mySend/index.js
var app = getApp(), fetchApi = require("../../../../utils/httpPost.js"), common_Js = require("../../../../component/common.js")
import Toast from '../../../../component/vant/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    recordList:[],
    pageIndex:1,
    pageSize:10,
    loading: false,
    netWork: true,
    isLoading: true,
    netWork:false,
    hasData:false,
    searchValue:'',
    roomId:0,
    isShowPopup: false,
    chooseItem: null,
    fileList0:[],
    radioType: 1,
    remark:'',
    state:0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.roomId){
      this.data.roomId = options.roomId
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initRequest()
  },
  
  handleChange({detail}){    
    console.log('detail',detail)
    this.data.pageIndex = 1;
    this.setData({
      recordList : [],
      currentTab: detail.index
    })
    this.data.currentTab = detail.index;
    this.data.state = detail.index;
    this.onHttpInit();
  },  
  onSearch(val){
    console.log('onSearch',val);
    this.data.searchValue = val.detail;
    this.data.pageIndex = 1;
    this.setData({
      recordList: [],
    })
    this.onHttpInit();
  },
  onSearchClick(val){
    console.log('onSearchClick',val)
    this.data.pageIndex = 1;
    this.setData({
      recordList: [],
    })
    this.onHttpInit();
  },
  initRequest(){
    var that = this;
    that.data.pageIndex = 1;
    that.setData({
      loading: false,
      netWork: false,
      isLoading: true,
      hasData:false,
      recordList:[]
    });
    that.onHttpInit()
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this;
    fetchApi.httpPost('/customer/call/record/list', {
      state: that.data.state,
      room_id: that.data.roomId,
      pageSize: that.data.pageSize,
      pageIndex: that.data.pageIndex,
    }, function (res) {
      if (res.data.code == 0) {
        if (res.data.totalcount > 0) {
          that.data.pageIndex += 1;
          console.log("访客登记记录列表", res)
          var records = res.data.data.recordList;
          if(records.length > 0){
            for (var index in records) {
              that.data.recordList.splice(that.data.recordList.length, 0, records[index]);
            }
            that.setData({
              hasData: true,
              recordList: that.data.recordList
            })
          }
        }
      }
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  goUploadImg(e){
    let item = e.currentTarget.dataset.item;
    this.data.chooseItem = item;
    this.setData({
      isShowPopup: true,
      chooseItem: item,
      remark: this.data.chooseItem.remark
    })
  },
  onChangeRemark(e){
    console.log('onChangeRemark',e);
    this.data.remark = e.detail;
  },
  onRadioChange(e){
    console.log('onRadioChange',e);
    this.data.radioType = e.detail;
  },
  closePop(){
    this.data.chooseItem = null;
    this.data.remark = '';
    this.setData({
      isShowPopup: false,
      remark: '',
      radioType: 1,
    })
  },  
  onConfirm(){
    var that = this;
    if(!that.data.chooseItem){
      Toast('请选择处理记录')
      return;
    }
    if(that.data.remark == '' || that.data.remark == undefined){
      Toast('请输入备注信息')
      return;
    }
    this.onHttpSubmitUpdateRecord()
  },
  onHttpSubmitUpdateRecord: function () {
    var that = this;
    fetchApi.httpPost('/customer/call/record/update', {
      record_id: that.data.chooseItem.id,
      state: that.data.radioType,
      remark: that.data.remark,
    }, function (res) {
      console.log("呼叫响应", res)
      if (res.data.code == 0) {
        let record_id = that.data.chooseItem.id;
        if (record_id > 0 && that.data.fileList0.length > 0) {
          for (var index in that.data.fileList0) {
            wx.uploadFile({
              url: fetchApi.getPlatformUrl() + '/customer/call/record/uploadImg',
              filePath: that.data.fileList0[index].url,
              name: 'file',
              formData: { record_id: record_id},
              success: function (res) {
                console.log('uploadFile success res', res)
              }, fail: function (res) {
                console.log('uploadFile fail res', res)
              }
            })
          }
        }
        that.setData({
          isShowPopup: false,
          remark: '',
          radioType: 1,
        })
        that.data.chooseItem = null;
        that.data.remark = '';
        that.initRequest();
      }else{
        Toast.fail(res.data.msg)
      }
    }, function () {
      that.setData({ netWork: false, hasData: false,loading: true,  isLoading: false, })
    })
  },  
  preImg: function (t) {
    console.log("preSunImg", t)
    var a = t.currentTarget.dataset.imageindex, e = t.currentTarget.dataset.imgdata || [];
    var c = [];
    for (var index in e) {
      c.push(e[index])
    }
    common_Js.preImg(a, c)
  },
  deleteImg0:function(res){
    console.log('删除图片',res)
    this.data.fileList0 = []
    this.setData({
      fileList0:[]
    })
  },
  afterRead(event) {
    console.log('afterRead',event)
    const { file, name } = event.detail;
    console.log('afterRead name',name)
    console.log('afterRead file',file)
    const fileList = this.data[`fileList${name}`];
    this.setData({ [`fileList${name}`]: fileList.concat(file) });
    console.log('afterRead fileList',fileList)
    if(name == 0){
      this.data.fileList0.concat(file)
      console.log('fileList0',this.data.fileList0)
    }
  },
  oversize() {
    wx.showToast({ title: '文件超出大小限制', icon: 'none' });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作  下拉刷新
   */
  onPullDownRefresh: function () {
    this.initRequest()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})